# PowerShell 测试脚本 - 解决中文乱码问题
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "🧪 运行单元测试和生成覆盖率报告..." -ForegroundColor Green

# 进入后端目录
Set-Location backend

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "1. 清理项目" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
& mvn clean

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "2. 运行测试并生成覆盖率报告" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
& mvn test

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "3. 生成JaCoCo覆盖率报告" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
& mvn jacoco:report

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "✅ 测试完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "📊 测试结果位置:" -ForegroundColor Cyan
Write-Host "- 测试报告: target\surefire-reports\" -ForegroundColor White
Write-Host "- 覆盖率报告: target\site\jacoco\index.html" -ForegroundColor White
Write-Host ""

# 检查覆盖率报告是否存在
$reportPath = "target\site\jacoco\index.html"
if (Test-Path $reportPath) {
    Write-Host "🚀 打开覆盖率报告..." -ForegroundColor Green
    Start-Process $reportPath
} else {
    Write-Host "⚠️ 覆盖率报告未找到，请检查测试是否成功执行" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "按任意键继续..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# 返回原目录
Set-Location ..